package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import myz.date.gui.CalendarPad;
import myz.date.gui.NumberLenghtLimitedDmt;
import db.domain.Student;
import db.domain.User;
import db.sqldao.StudentSqlDao;
import db.sqldao.UserSqlDao;

/**
 * 充值，挂失界面
 * 
 * @author 冀鲁
 * @date 2011-06-29
 * @version 1.0.0.0
 * 
 */
@SuppressWarnings("serial")
public class UserManagerForm extends JDialog implements MouseListener,
		ActionListener {
	private JLabel lblName;// 用户名
	private JTextField txtName;// 用户名
	private JLabel lblPassword;// 密码
	private JTextField txtPassword;// 密码
	private JLabel lblOldPassword;// 旧 密码
	private JTextField txtOldPassword;// 旧密码
	private JPanel jp;
	private JLabel lblLose;
	private JRadioButton rdbLose[];// 挂失

	private JButton set;// 设置
	private JLabel lblPicture;// 系统图片
	private String type;

	// Student Student = new Student();

	public UserManagerForm(String type) {
		// 添加一些组件，添加到jp上
		int width = 600, height = 450;
		int x0 = 15, y0 = 50, w0 = 65, h0 = 20;
		int x1 = 100, y1 = 50;
		int w1 = 120, ww = 30, hh = 150;

		this.type = type;
		this.setTitle("管理员管理界面");
		this.setLayout(null);
		jp = new JPanel(null);// 使用坐标布局

		TitledBorder tb = new TitledBorder("校园卡余额信息");// 标题
		tb.setTitleColor(Color.red);// 标题颜色
		// 标题字体
		tb.setTitleFont(new Font("黑体", Font.BOLD, 20));
		// 边框颜色和宽度
		tb.setBorder(new LineBorder(Color.blue, 2, false));// false直角true 圆角
		jp.setBorder(tb);
		jp.setBounds(0, 0, width, height);
		this.add(jp);

		// Student = student;
		// 第一行
		lblName = new JLabel("用户名：");
		lblName.setHorizontalAlignment(JLabel.RIGHT);// 对齐方式
		// lblNo.setBorder(new LineBorder(Color.red, 1, false));
		lblName.setBounds(x0, y0, w0, h0);
		jp.add(lblName);
		txtName = new JTextField();
		txtName.setBounds(x0 + x1, y0, w1, h0);
		txtName.setBorder(new LineBorder(Color.black, 1, true));
		// txtName.setDocument(new NumberLenghtLimitedDmt(5));
		// txtName.setEnabled(false);// 文本框不可编辑
		jp.add(txtName);

		// 第二行
		lblPassword = new JLabel("密码：");
		lblPassword.setHorizontalAlignment(JLabel.RIGHT);// 对齐方式
		// lblNo.setBorder(new LineBorder(Color.red, 1, false));
		lblPassword.setBounds(x0, y0 + y1, w0, h0);
		jp.add(lblPassword);

		txtPassword = new JTextField();
		txtPassword.setBounds(x0 + x1, y0 + y1, w1, h0);
		txtPassword.setBorder(new LineBorder(Color.black, 1, true));
		// txtPassword.setDocument(new NumberLenghtLimitedDmt(5));
		// txtPassword.setText("0");
		jp.add(txtPassword);

		// 第二行
		lblOldPassword = new JLabel("旧密码：");
		lblOldPassword.setHorizontalAlignment(JLabel.RIGHT);// 对齐方式
		// lblNo.setBorder(new LineBorder(Color.red, 1, false));
		lblOldPassword.setBounds(x0, y0 + y1 * 2, w0, h0);

		txtOldPassword = new JTextField();
		txtOldPassword.setBounds(x0 + x1, y0 + y1 * 2, w1, h0);
		txtOldPassword.setBorder(new LineBorder(Color.black, 1, true));
		// txtPassword.setDocument(new NumberLenghtLimitedDmt(5));
		//txtOldPassword.setText("0");
		if ("modify".equals(type)) {
			jp.add(lblOldPassword);
			jp.add(txtOldPassword);
		}

		// 第三行
		lblLose = new JLabel("挂失：");
		lblLose.setHorizontalAlignment(JLabel.RIGHT);
		// lblLose.setBorder(new LineBorder(Color.red, 1, false));
		lblLose.setBounds(x0, 20 + h0 + hh, w0, h0);
		jp.add(lblLose);

		String[] strSex = { "是", "否" };
		rdbLose = new JRadioButton[strSex.length];
		ButtonGroup bg = new ButtonGroup();// 按钮组（和单选有关）
		for (int i = 0; i < strSex.length; i++) {
			rdbLose[i] = new JRadioButton(strSex[i]);
			rdbLose[i].setBorder(new LineBorder(Color.red, 1, false));
			bg.add(rdbLose[i]);
			jp.add(rdbLose[i]);
			rdbLose[i].setBounds(lblLose.getX() + w0 + 45 * i + 35, lblLose
					.getY(), 45, h0);
		}
		rdbLose[0].setSelected(true);// 被选择

		// 第四行
		set = new JButton("设置");
		set.setBounds(130, 350, 80, 30);
		set.addActionListener(this);

		jp.add(set);

		this.setModal(true);// 窗口独占模式（JDialog独有）
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		// 匿名内部类
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				closeWindow();
			}
		});

		// displayStudent(student);

		// 设置图片的位置
		lblPicture = new JLabel("<html>　　系<br>　　统<br>　　图<br>　　片<br></html>");
		// lblPicture.setBorder(new LineBorder(Color.blue, 5, true));
		lblPicture.setBounds(0, 0, width, height);// 设置图片位置
		displayPictur displayPictur = new displayPictur(4, lblPicture);
		jp.add(lblPicture);

		this.setSize(width, height);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setVisible(true);
	}

	protected void closeWindow() {
		// 关闭窗口
		this.dispose();// 释放本窗口资源
	}

	public void mouseClicked(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON1/* 左键 */) {
			// 获取鼠标的位置
			double x = e.getLocationOnScreen().getX();
			double y = e.getLocationOnScreen().getY();
			CalendarPad cp = new CalendarPad(2010, 4, 27, x, y);

		}
	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {

	}

	public void mouseReleased(MouseEvent e) {

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == set) {
			set();
		}
	}

	/**
	 * 设置
	 */
	private void set() {

		if ("add".equals(type)) {
			add();
		} else if ("delete".equals(type)) {
			delete();
		} else if ("modify".equals(type)) {
			modify();
		}

		// //boolean flag = sDao.updateStudent(Student);
		// if (flag) {
		// JOptionPane.showMessageDialog(this, "恭喜你【设置】 成功！");
		// } else {
		// JOptionPane.showMessageDialog(this, "对不起【设置】 失败！");
		// }
	}

	/**
	 * 修改
	 */
	private void modify() {

		// (1)先判断用户名是否存在，若存在不能进行修改
		UserSqlDao uDao = new UserSqlDao();
		ArrayList<User> al = uDao.getUser("where name='"
				+ txtName.getText().trim() + "'");// binary是二进制转换用来区分大小写
		if (al.size() <= 0) {
			JOptionPane.showMessageDialog(this, "对不起，【用户名】不存在，请重新输入！");
			// 文本框中的内容自动选中
			txtName.setSelectionStart(0);
			txtName.setSelectionEnd(txtName.getText().length());
			txtName.requestFocus(true);
			return;
		}
		if (!txtOldPassword.getText().equals(al.get(0).getPassword())) {
			JOptionPane.showMessageDialog(this, "对不起，【旧密码】错误，请重新输入！");
			return;
		}

		// (2)使用jdbc技术，将对象信息保存到数据库中

		al.get(0).setName(txtName.getText());
		al.get(0).setPassword(txtPassword.getText());
		boolean flag = uDao.updateUser(al.get(0));
		if (flag) {
			JOptionPane.showMessageDialog(this, "恭喜你【修改管理员信息】 成功！");
		} else {
			JOptionPane.showMessageDialog(this, "对不起【修改管理员信息】 失败！");
		}
	}

	/**
	 * 删除
	 */
	private void delete() {
		// (1)先判断用户名是否存在，若存在不能进行删除
		UserSqlDao uDao = new UserSqlDao();
		ArrayList<User> al = uDao.getUser("where name='"
				+ txtName.getText().trim() + "'");// binary是二进制转换用来区分大小写
		if (al.size() <= 0) {
			JOptionPane.showMessageDialog(this, "对不起，【用户名】不存在，请重新输入！");
			// 文本框中的内容自动选中
			txtName.setSelectionStart(0);
			txtName.setSelectionEnd(txtName.getText().length());
			txtName.requestFocus(true);
			return;
		}
		if (!txtPassword.getText().equals(al.get(0).getPassword())) {
			JOptionPane.showMessageDialog(this, "对不起，【密码】错误，请重新输入！");
			return;
		}

		int yes = JOptionPane.showConfirmDialog(this, "您确认删除该管理员吗？", "删除提示",
				JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if (yes == JOptionPane.YES_OPTION) {
			boolean flag = uDao.deleteUser(al.get(0).getNo());
			if (flag) {
				JOptionPane.showMessageDialog(this, "恭喜你【删除】成功！");
			} else {
				JOptionPane.showMessageDialog(this, "对不起【删除】失败！");
			}
		}

	}

	/**
	 * 添加
	 */
	private void add() {
		User user = new User();
		// (1)先判断姓名是否存在，若存在不能进行添加
		UserSqlDao uDao = new UserSqlDao();
		ArrayList<User> al = uDao.getUser("where name='"
				+ txtName.getText().trim() + "'");// binary是二进制转换用来区分大小写
		if (al.size() > 0) {
			JOptionPane.showMessageDialog(this, "对不起，【用户名】已经存在，请重新输入！");
			// 文本框中的内容自动选中
			txtName.setSelectionStart(0);
			txtName.setSelectionEnd(txtName.getText().length());
			txtName.requestFocus(true);
			return;
		} else if ("".equals(txtPassword.getText())) {
			JOptionPane.showMessageDialog(this, "对不起，【密码】不能为空，请重新输入！");
			return;
		}
		user.setName(txtName.getText());
		user.setPassword(txtPassword.getText());
		// (2)使用jdbc技术，将对象信息保存到数据库中
		boolean flag = uDao.addUser(user);
		if (flag) {
			JOptionPane.showMessageDialog(this, "恭喜你【添加学生信息】 成功！");

		} else {
			JOptionPane.showMessageDialog(this, "对不起【添加学生信息】 失败！");
		}
	}

	/**
	 * 将余额，挂失信息显示到窗口的组件上
	 * 
	 * @param student
	 */
	private void displayStudent(Student student) {
		txtName.setText(Integer.toString(student.getMoney()));// 余额
		String lose = student.getLose();// 丢失
		for (int i = 0; i < rdbLose.length; i++) {
			if (lose.equals(rdbLose[i].getText())) {
				rdbLose[i].setSelected(true);
				break;
			}
		}
	}

}
